/**
 
 * 获取个人详细信息 
 */

import { getBaseUrl } from '@/services/urlconst';
import { getCompanyID, getPosIDs, getSystemTypeValue, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { message } from 'antd';
import { useMemo } from 'react';
import { useRequest } from 'umi';
let baseUrl = getBaseUrl();

/**
 * 获取用户详情
 * @returns
 */
const reloadSelfInfoService = () => {
  const reloadSelfInfo = useRequest(
    (payload: any) => {
      let reqUrl = `${baseUrl}/commonController/reloadSelfInfo`;
      let methodType = 'POST';
      const bodyParams = {
        id: payload?.id,
        systemType: getSystemTypeValue(),
      };
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        if (!res?.msgFlag) {
          message.error(res?.msg);
        }
      },
    },
  );
  /**
   * 返回用户数据
   */
  const reloadSelfInfoObj = useMemo(() => {
    return reloadSelfInfo?.data?.obj;
  }, [reloadSelfInfo?.data]);
  return {
    reloadSelfInfoLoading: reloadSelfInfo?.loading,
    reloadSelfInfoRun: reloadSelfInfo?.run,
    reloadSelfInfoObj: reloadSelfInfoObj,
  };
};

/**
 * 个人中心 修改密码
 * @returns
 */
const modifyPwdService = () => {
  const modifyPwd = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/commonController/modifyPwd`;
      let methodType = 'PUT';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        if (res && res?.msgFlag) {
          message.success(`${res?.msg}。下次登录请使用新密码登录`);
        } else {
          message.error(res?.msg);
        }
      },
    },
  );
  return {
    modifyPwdRun: modifyPwd?.run,
    modifyPwdLoading: modifyPwd?.loading,
  };
};

/**
 * 个人中心 修改资料
 * @param modifySelfCallback 接口回调
 * @returns
 */
const modifySelfService = (modifySelfCallback: (res: any) => void) => {
  const modifySelf = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/commonController/modifySelf`;
      let methodType = 'PUT';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: modifySelfCallback,
    },
  );
  return {
    modifySelfRun: modifySelf?.run,
    modifySelfLoading: modifySelf?.loading,
  };
};

export { reloadSelfInfoService, modifyPwdService, modifySelfService };
